package bag.filter;

import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2017/11/13.
 */
@WebFilter(filterName = "MyFilter",urlPatterns = "/*",dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
public class MyFilter implements Filter {
    private Logger logger = Logger.getLogger(MyFilter.class);
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("run");
        HttpServletRequest request = (HttpServletRequest) req;
        HttpSession session = request.getSession();
        Object admin = session.getAttribute("admin");
        if(admin==null){
            request.setAttribute("msg","请先登录");
            request.getRequestDispatcher("/login.jsp").forward(req,resp);
        }else{
            chain.doFilter(req,resp);
        }
        /*HttpServletRequest request = (HttpServletRequest) req;
        logger.info("filter......"+request.getServletPath());
        chain.doFilter(req,resp);*/
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
